package trans

import (
	"fmt"
	"strconv"
	"time"
)

//设置时区
var Location =  "Asia/Shanghai"

//获取时区
func GetLocal() (*time.Location, error) {
	return  time.LoadLocation( Location )
}

//string unix time to time date
func StringUnixTimeToDate(str string) time.Time {
	st, _ := strconv.Atoi(str)
	local, _ := GetLocal()
	dt, _ := time.ParseInLocation("2006-01-02", time.Unix(int64(st), 0).Format("2006-01-02"), local)
	//dt := time.Unix(int64(st), 0).Format("2006-01-02")
	fmt.Println("时间[%v]", dt)
	return dt
}

//string date to time date
func StringToDate(str string) time.Time {
	var dt time.Time
	local, _ := GetLocal()
	if len(str) == len("2006-01-02") {
		dt, _ = time.ParseInLocation("2006-01-02", str, local)
		//InfoLog("时间[%v]", dt)
		return dt

	} else if len(str) == len("2006-01-02  15:04:05") {
		dt, _ = time.ParseInLocation("2006-01-02  15:04:05", str, local)
		//InfoLog("时间[%v]", dt)
		return dt
	}
	return dt
}

func DateToString(t time.Time) string {
	local, _ := GetLocal()
	return t.In(local).Format("20060102")
}

//获取当日零点时间
func GetCurUnixDate() int64 {
	local, _ := GetLocal()
	timeStr := time.Now().In(local).Format("2006-01-02")
	t, _ := time.ParseInLocation("2006-01-02", timeStr, local)
	return t.Unix()
}

//获取指定日期的起始时间(0点)
func GetUnixDateBegin(str string) int64 {
	//8小时时差
	//d, _ := time.ParseDuration("-8h")
	//tmp, _ := time.Parse("2006-01-02", str)
	//return tmp.Add(d).Unix()
	local, _ := GetLocal()
	tmp, _ := time.ParseInLocation("2006-01-02", str, local)
	return tmp.Unix()
}

//获取指定日期的终止时间(后一天0点)
func GetUnixDateEnd(str string) int64 {
	//8小时时差
	//d, _ := time.ParseDuration("-8h")
	//tmp, _ := time.Parse("2006-01-02", str)
	//return tmp.AddDate(0, 0, 1).Add(d).Unix()
	local, _ := GetLocal()
	tmp, _ := time.ParseInLocation("2006-01-02", str, local)
	return tmp.AddDate(0, 0, 1).Unix()
}

//获取当前日期
func GetCurDate() (timeStr string) {
	local, _ := GetLocal()
	timeStr = time.Now().In(local).Format("2006-01-02")
	return timeStr
}

//获取当前日期
func GetPrevDate() string {
	sdate := GetCurDate()
	return GetSDateNextDate(sdate, -1)
}

//获取当前日期
func GetCurTDate() (tdate time.Time) {
	local, _ := GetLocal()
	tdate, _ = time.ParseInLocation("2006-01-02",
			time.Now().Format("2006-01-02"), local)
	return tdate
}

//获取当前日期后N天, 负数为减
func GetCurNextDate(days int) (ndate string) {
	local, _ := GetLocal()
	ndate = time.Now().In(local).AddDate(0, 0, days).Format("2006-01-02")
	return ndate
}

//获取某字符日期后N天, 负数为减
func GetSDateNextDate(sday string, days int) string {
	local, _ := GetLocal()
	day := StringToDate(sday)
	nday := day.In(local).AddDate(0, 0, days).Format("2006-01-02")
	return nday
}

//获取某日期后N天, 负数为减
func GetDDateNextDate(tday time.Time, days int) string {
	local, _ := GetLocal()
	return tday.In(local).AddDate(0, 0, days).Format("2006-01-02")
}

//获取当前日期时间
func GetCurDateTime() string {
	local, _ := GetLocal()
	timeStr := time.Now().In(local).Format("2006-01-02 15:04:05")
	return timeStr
}

//获取当前日期时间
func GetFormatCurDateTime(format string) string {
	local, _ := GetLocal()
	timeStr := time.Now().In(local).Format(format)
	return timeStr
}

//int64转datetime
func Int64ToDateTime(itm int64) string {
	local, _ := GetLocal()
	return time.Unix(itm, 0).In(local).Format("2006-01-02 15:04:05")
}

//计算两个日期差
func DiffDate(date1 string, date2 string) int {
	local, _ := GetLocal()
	dt1 := StringToDate(date1).In(local)
	dt2 := StringToDate(date2).In(local)
	hours := dt1.Sub(dt2).Hours()

	//return int(math.Abs(hours / 24))
	return int(hours / 24)
}

